<?php

include_once(DATATYPES.'/DatatypeBase.php');

class F {
	
	static $factory;
	static $dbVars;
	
	static $config;
	static $paths;
	static $db;
	static $tpl;
	static $nav;
	static $controller;
	static $auth;
	static $crypt;
	static $apiCaller;
	
	
	static $classLoader = array();
	
	public static function get(){
		if( !isset( self::$factory ) ){
			self::$factory = new F();
		}
		return self::$factory;
	}
	
	function Controller($controller = null){
		if( is_null($controller) ){
			return self::$controller;
		}else{
			self::$controller = $controller;
		}
	}
	function DB(){
		if( !isset( self::$db ) ){
			self::$db = new DB();
		}	
		return self::$db;
	}
	function Config(){
		if( !isset( self::$config ) ){
			include_once(ROOT.'config.php');
			self::$config = new Config();
		}	
		return self::$config;
	}
	function Paths(){
		if( !isset( self::$paths ) ){
			include_once(CLASSES.'Paths.php');
			self::$paths = new Paths();
		}	
		return self::$paths;
	}
	function Tpl(){
		if( !isset( self::$tpl ) ){
			include_once(CLASSES.'Tpl.php');
			self::$tpl = new Tpl();
		}	
		return self::$tpl;
	}
	function Crypt(){
		if( !isset( self::$crypt ) ){
			include_once(CLASSES.'Crypt.php');
			self::$crypt = new Crypt();
		}	
		return self::$crypt;
	}
	function Auth(){
		if( !isset( self::$auth ) ){
			include_once(CLASSES.'Auth.php');
			self::$auth = new Auth();
		}	
		return self::$auth;
	}
	function Nav(){
		if( !isset( self::$nav ) ){
			include_once(CLASSES.'Navigation.php');
			self::$nav = new Navigation();
		}	
		return self::$nav;
	}
	
	function API($appID, $encodedReq, $methodToCall = null){
		include_once(CLASSES.'API.php');
		return  new API($appID,$encodedReq, $methodToCall);
	}
	function APICaller(){
		if( !isset( self::$apiCaller ) ){
			include_once(CLASSES.'APICaller.php');
			self::$apiCaller = new APICaller();
		}	
		return self::$apiCaller;
	}
	
	/**
	 * 
	 * @param $fileArray Array File used for the upload  
	 * @return Object Upload obj to handle uploads 
	 */
	function Upload($fileArray){
		include_once(TPARTY.'php/upload/class.upload.php');
    return new Upload($fileArray['tmp_name']);
	}
	
	
	
	function DataTypeForms(){
		include_once(CLASSES.'DataTypeForms.php');
		return new DataTypeForms();
	}
	
	
	/* Load Classes from the tables */
	function loader($className, $columnID = null, $load = false, $column = 'id'){
		$newClassName = '_'.$className;
		include_once(TABLES.$newClassName.'.php');
		return new $newClassName($columnID, $load, $column );
	}
	
	/* Datatypes */
	function Datatype($column, $type, $default = null, $relationArray = array() , $index = '', $lengthOveride = '', $allowNULL = true){
		$className = $type."_Datatype";
		include_once(DATATYPES.''.$type.'.php');
		return new $className($column, $default, $relationArray, $index, $lengthOveride , $allowNULL);
	}
	
}

?>
